package com.union.core.sys.service;


import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.union.core.sys.dao.RoleDao;
import com.union.core.sys.dao.impl.RoleDaoImpl;
import com.union.core.sys.dao.impl.SysManagerDaoImpl;
import com.union.core.sys.dao.impl.SysModuleDaoImpl;
import com.union.core.sys.dao.impl.SysModuleTypeDaoImpl;
import com.union.core.sys.model.Role;
import com.union.core.sys.model.SysManager;
import com.union.core.sys.model.SysModule;
import com.union.core.sys.model.SysModuleType;

/**
 * 服务: 角色
 * filename: RoleService.java
 * project:  union
 * @company Hainan Jowe Technology Ltd
 * @author Harry Zhang
 * @create in 2012-10-29
 * @version 1.0
 */
public class RoleService {
	private RoleDao rdi;
	private SysModuleTypeDaoImpl smtdi;
	private SysModuleDaoImpl smdi;
	private SysManagerDaoImpl sysmdi;
	private static Logger log = Logger.getLogger(RoleService.class.getName());
	public RoleService(){
		rdi=new RoleDaoImpl();
		sysmdi=new SysManagerDaoImpl();
		smdi=new SysModuleDaoImpl();
		smtdi=new SysModuleTypeDaoImpl();
	}
	public boolean addRole(Role role) {
		boolean bl=rdi.addRole(role);
		log.info("添加角色"+role.getRole_name());
		return bl;
	}

	public boolean delRole(int roleId) {
		boolean bl=rdi.delRole(roleId);
		//搜索账号表，返回被赋予此角色的账号列表
		String roleIdstr="|"+roleId+"|";
		
		List<SysManager> managerList=sysmdi.getManagerListByRoleId(roleIdstr);
		//遍历返回的账号列表
		for(SysManager manager: managerList){
			String rod="|";
			String roles=manager.getMANAGER_ROLES();
			if(roles!=null && !"".equals(roles)){
				String result[]=roles.split("|");
				for(String rs : result){
					if (!rs.equals(String.valueOf(roleId))){
						rod=rod+rs;
					}
				}
				manager.setMANAGER_ROLES(rod);
				sysmdi.modifyManager(manager);
			}
		}
		log.info("删除角色id："+roleId);
		return bl;
	}

	public List<Role> queryRoleList(int roleId, String roleName,
			String addManager, String addTime, String parName,int roleType,int org_id, String parVal,
			String orderBy, int start, int rownum) {
			List<Role> roleList=rdi.queryRoleList(roleId, roleName, addManager, addTime, parName, roleType, org_id, parVal, orderBy, start, rownum);
			log.info("查询角色列表");
		return roleList;
	}
	public int queryRoleListCount(int roleId, String roleName,
			String addManager, String addTime, String parName,int roleType,int org_id, String parVal) {
			int count=rdi.queryRoleListCount(roleId, roleName, addManager, addTime, parName, roleType, org_id, parVal);
			log.info("查询角色记录数");
		return count;
	}
	public boolean update(Role role) {
		// TODO Auto-generated method stub
		return false;
	}
	public Role queryById(int roleId) {
		Role role=rdi.queryByid(roleId);
		log.info("查询单条记录id为："+roleId);
		return role;
	}
	public List<SysModuleType> editload(int roleId,String MODULE_MGR_PURVIEW){
		String pulist=rdi.queryByid(roleId).getRole_permissions();
		List<String> sysModuleIdList=new ArrayList<String>();
		if(pulist!=null&&!"".equals(pulist)){
			sysModuleIdList = sysmdi.getModuleMgrPurviewList(pulist);
		}
		List<String> sysModuleIdList1 = sysmdi.getModuleMgrPurviewList(MODULE_MGR_PURVIEW);
		/*取得模块类别*/
		List<SysModuleType> sysModuleTypeList = smtdi.getModuleTypeList(1);
		List<SysModuleType> sysModuleTypeList1=new ArrayList<SysModuleType>();
		for(int i=0;i<sysModuleTypeList.size();i++){//遍历所有模块类型
			/*取得各类别小模块*/
			SysModuleType sysModuleType = (SysModuleType)sysModuleTypeList.get(i);
			List<SysModule> sysModuleList = smdi.getModuleList(1,2,sysModuleType.getMODULE_TYPE_ID());
			List<SysModule> sysModuleList1=new ArrayList<SysModule>();
			for(int j=0;j<sysModuleList.size();j++){//遍历第i个模块里的所有子模块
				String sysModuleIdTemp = null;
				for(int s=0;s<sysModuleIdList1.size();s++){//遍历用户权限中的模块ID
					sysModuleIdTemp = (String)sysModuleIdList1.get(s);
					if(sysModuleIdTemp!=null){
							//从所有子模块中取得用户拥有权限的子模块
							if((String.valueOf(sysModuleList.get(j).getMODULE_ID())).equals(sysModuleIdTemp)){
								for(int b=0;b<sysModuleIdList.size();b++){//遍历用户选中角色拥有权限的模块ID
									String sysModuleIdrole=(String)sysModuleIdList.get(b);
									if(sysModuleIdrole.equals(sysModuleIdTemp)){
										sysModuleList.get(j).setIfchecked(1);//从用户的子模中将角色的子模设置为选中
									}
								}
								sysModuleList1.add(sysModuleList.get(j));
								
							}
					}
				}
				
			}
			if(sysModuleList1.size()!=0){
				sysModuleType.setSysModuleList(sysModuleList1);
				sysModuleTypeList1.add(sysModuleType);
			}
			
		}
		log.info("加载角色赋权");
		return sysModuleTypeList1;
	}
	public boolean empower(int roleId,String[] MODULE_MGR_PURVIEW){
		boolean bl=false;
		String rolePermissions="";
		if(MODULE_MGR_PURVIEW.length!=0 && MODULE_MGR_PURVIEW!=null){
			rolePermissions="|";
			for(String s : MODULE_MGR_PURVIEW){
				rolePermissions=rolePermissions+s+"|";
			}
		}
		Role role=rdi.queryByid(roleId);
		role.setRole_permissions(rolePermissions);
		bl=rdi.update(role);
		log.info("为角色赋权");
		return bl;
	}
	/**
	 * 校验登陆用户帐号是否已存在
	 * @param loginName String
	 * @return boolean
	 * false-无记录 true-有记录
	 */
	public boolean checkRoleName(String roleName){
		log.info("PLAT: 校验角色名是否已存在: "+roleName);
		return rdi.checkRoleName(roleName);
	}
}
